package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.entity.form.ProfitRecordQueryForm;
import com.springboot.cloud.demos.entity.param.ProfitRecordQueryParam;
import com.springboot.cloud.demos.entity.param.UserQueryParam;
import com.springboot.cloud.demos.entity.po.ProfitRecord;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.producer.dao.ProfitRecordMapper;
import com.springboot.cloud.demos.producer.service.IProfitRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RefreshScope
public class ProfitRecordServiceImpl extends ServiceImpl<ProfitRecordMapper, ProfitRecord> implements IProfitRecordService {

    @Value("${producer.profitRecord:123}")
    private String value;

    @Override
    @CacheEvict(value = "profitRecord", allEntries = true)
    public boolean add(ProfitRecord profitRecord) {
        return this.save(profitRecord);
    }

    @Override
    @CacheEvict(value = "profitRecord", key = "#root.targetClass+'-'+#id")
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    public IPage list(ProfitRecordQueryForm profitRecordQueryForm) {
        ProfitRecordQueryParam userQueryParam = profitRecordQueryForm.toParam(ProfitRecordQueryParam.class);
        Page<ProfitRecord> page = new Page<>(profitRecordQueryForm.getCurrent(), profitRecordQueryForm.getSize());
        QueryWrapper<ProfitRecord> queryWrapper = userQueryParam.build();
        if (StringUtils.isNotEmpty(userQueryParam.getUserId())) {
            queryWrapper.eq("user_id", userQueryParam.getUserId());
        } if (StringUtils.isNotEmpty(userQueryParam.getType())) {
            queryWrapper.eq("type", userQueryParam.getType());
        }
        queryWrapper.orderByDesc("created_time");
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    @CacheEvict(value = "profitRecord", key = "#root.targetClass+'-'+#profitRecord.id")
    public boolean update(ProfitRecord profitRecord) {
        return this.updateById(profitRecord);
    }

    @Override
    @Cacheable(value = "profitRecord", key = "#root.targetClass+'-'+#id")
    public ProfitRecord get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    @Cacheable(value = "profitRecord", key = "#root.targetClass+'-user-'+#param.userId")
    public List<ProfitRecord> query(ProfitRecordQueryParam param) {
        QueryWrapper<ProfitRecord> queryWrapper = param.build();
        queryWrapper.eq("user_id", param.getUserId());
        queryWrapper.orderByAsc("created_time");
        return this.list(queryWrapper);
    }
}
